package com.sgzn.sgpay.web.controller;

import com.alibaba.fastjson.JSON;
import com.sgzn.sgpay.enums.AppName;
import com.sgzn.sgpay.model.request.*;
import com.sgzn.sgpay.model.request.util.PageQueryParam;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.model.response.result.ExchangeCoinInfoQueryPageResult;
import com.sgzn.sgpay.persistence.domain.TExchangeCoinInfo;
import com.sgzn.sgpay.service.SgsExchangeCoinInfoService;
import com.sgzn.sgpay.util.SerialNoUtil;
import com.sgzn.sgpay.web.controller.base.MerchantBaseController;
import com.sgzn.sgpay.web.handle.AssembleCommonHandle;
import com.sgzn.sgpay.web.request.Impl.*;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 兑换比例管理控制器
 * @author huangsenguang
 * @version Id: ExchangeCoinInfoController.java, v 0.1 2018/5/1 10:41 huangsenguang Exp $$
 */
@Slf4j
@Controller
@RequestMapping("/merchant/exchangeCoinInfo")
public class ExchangeCoinInfoController extends MerchantBaseController {
    
    @Resource
    private SgsExchangeCoinInfoService sgsExchangeCoinInfoService;
    @Resource
    private AssembleCommonHandle assembleCommonHandle;

    /**
     * 添加兑换比例管理
     * @param request
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public SgsResultResponse<String> addExchangeCoinInfo(ExchangeCoinInfoAddRequest request) {
        super.setMchInfo(request);
        return this.sgsExchangeCoinInfoService.addExchangeCoinInfo(assembleAddRequest(request));
    }
    
    /**
     * 删除兑换比例管理
     * @param request
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public SgsResultResponse<String> deleteExchangeCoinInfo(ExchangeCoinInfoDeleteRequest request) {
        super.setMchInfo(request);
        return this.sgsExchangeCoinInfoService.deleteExchangeCoinInfo(assembleDeleteRequest(request));
    }

    /**
     * 查询兑换比例管理的信息
     * @param request
     * @return
     */
    @RequestMapping("query")
    @ResponseBody
    public SgsResultResponse<TExchangeCoinInfo> queryExchangeCoinInfoInfo(ExchangeCoinInfoQueryByIdRequest request) {
        super.setMchInfo(request);
        return this.sgsExchangeCoinInfoService.queryExchangeCoinInfo(assembleQueryByIdRequest(request));
    }

    /**
     * 分页查询兑换比例管理的信息
     * @param request
     * @return
     */
    @RequestMapping("queryPage")
    @ResponseBody
    public SgsResultResponse<ExchangeCoinInfoQueryPageResult> queryPageExchangeCoinInfoInfo(ExchangeCoinInfoQueryPageRequest request) {
        super.setMchInfo(request);
        return this.sgsExchangeCoinInfoService.queryPage(assembleQueryPageRequest(request));
    }

    /**
     * 更新兑换比例管理信息
     * @param request
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public SgsResultResponse<String> updateExchangeCoinInfo(ExchangeCoinInfoUpdateRequest request) {
        super.setMchInfo(request);
        return this.sgsExchangeCoinInfoService.updateExchangeCoinInfo(assembleUpdateRequest(request));
    }

    private SgsExchangeCoinInfoAddRequest assembleAddRequest(ExchangeCoinInfoAddRequest request) {
        SgsExchangeCoinInfoAddRequest sgsExchangeCoinInfoAddRequest = new SgsExchangeCoinInfoAddRequest();
        this.assembleCommonHandle.assemble(request, sgsExchangeCoinInfoAddRequest);
        sgsExchangeCoinInfoAddRequest.setDeviceId(request.getDeviceId());
        sgsExchangeCoinInfoAddRequest.setExchangeValue(request.getExchangeValue());
        return sgsExchangeCoinInfoAddRequest;
    }

    private SgsExchangeCoinInfoDeleteRequest assembleDeleteRequest(ExchangeCoinInfoDeleteRequest request) {
        SgsExchangeCoinInfoDeleteRequest sgsExchangeCoinInfoDeleteRequest = new SgsExchangeCoinInfoDeleteRequest();
        this.assembleCommonHandle.assemble(request, sgsExchangeCoinInfoDeleteRequest);
        sgsExchangeCoinInfoDeleteRequest.setDeviceId(request.getDeviceId());
        return sgsExchangeCoinInfoDeleteRequest;
    }

    private SgsExchangeCoinInfoQueryRequest assembleQueryByIdRequest(ExchangeCoinInfoQueryByIdRequest request) {
        SgsExchangeCoinInfoQueryRequest sgsExchangeCoinInfoQueryRequest = new SgsExchangeCoinInfoQueryRequest();
        this.assembleCommonHandle.assemble(request, sgsExchangeCoinInfoQueryRequest);
        sgsExchangeCoinInfoQueryRequest.setDeviceId(request.getDeviceId());
        return sgsExchangeCoinInfoQueryRequest;
    }

    private SgsExchangeCoinInfoUpdateRequest assembleUpdateRequest(ExchangeCoinInfoUpdateRequest request) {
        SgsExchangeCoinInfoUpdateRequest sgsExchangeCoinInfoRequest = new SgsExchangeCoinInfoUpdateRequest();
        this.assembleCommonHandle.assemble(request, sgsExchangeCoinInfoRequest);
        sgsExchangeCoinInfoRequest.setDeviceId(request.getDeviceId());
        sgsExchangeCoinInfoRequest.setExchangeValue(request.getExchangeValue());
        return sgsExchangeCoinInfoRequest;
    }

    private SgsExchangeCoinInfoQueryPageRequest assembleQueryPageRequest(ExchangeCoinInfoQueryPageRequest request) {
        SgsExchangeCoinInfoQueryPageRequest sgsExchangeCoinInfoQueryPageRequest = new SgsExchangeCoinInfoQueryPageRequest();
        this.assembleCommonHandle.assemble(request, sgsExchangeCoinInfoQueryPageRequest);
        sgsExchangeCoinInfoQueryPageRequest.setDeviceId(request.getDeviceId());
        sgsExchangeCoinInfoQueryPageRequest.setExchangeValue(request.getExchangeValue());
        PageQueryParam pageQueryParam = new PageQueryParam();
        pageQueryParam.setPageSize(Integer.parseInt(request.getPageSize()));
        pageQueryParam.setCurrentPage(request.getPageNum());
        sgsExchangeCoinInfoQueryPageRequest.setPageQueryParam(pageQueryParam);
        return sgsExchangeCoinInfoQueryPageRequest;
    }
    
}

